﻿using System;
using System.IO;
using System.Windows.Threading;

namespace xCarLinkMP3Organizer
{
    class FSWWrapper : DispatcherObject, IDisposable
    {
        private FileSystemWatcher _fsw;

        public FSWWrapper(string path)
        {
            _fsw = new FileSystemWatcher(path);

            EnableEvents();
        }

        public FSWWrapper(string path, string filter)
        {
            _fsw = new FileSystemWatcher(path, filter);

            EnableEvents();
        }

        private void EnableEvents()
        {
            _fsw.Changed += OnFswChanged;
            _fsw.Created += OnFswCreated;
            _fsw.Deleted += OnFswDeleted;
            _fsw.Renamed += OnFswRenamed;
        }

        private void DisableEvents()
        {
            _fsw.Changed -= OnFswChanged;
            _fsw.Created -= OnFswCreated;
            _fsw.Deleted -= OnFswDeleted;
            _fsw.Renamed -= OnFswRenamed;
        }

        public NotifyFilters NotifyFilter
        {
            get { return _fsw.NotifyFilter; }
            set { _fsw.NotifyFilter = value; }
        }

        public bool EnableRaisingEvents
        {
            get { return _fsw.EnableRaisingEvents; }
            set { _fsw.EnableRaisingEvents = value; }
        }

        public string Filter
        {
            get { return _fsw.Filter; }
            set { _fsw.Filter = value; }
        }

        public bool IncludeSubdirectories
        {
            get { return _fsw.IncludeSubdirectories; }
            set { _fsw.IncludeSubdirectories = value; }
        }

        public event FileSystemEventHandler Changed;

        public event FileSystemEventHandler Created;

        public event FileSystemEventHandler Deleted;

        public event RenamedEventHandler Renamed;

        void OnFswRenamed(object sender, RenamedEventArgs e)
        {
            Dispatcher.BeginInvoke((Action<RenamedEventArgs>) OnRenamed, e);
        }

        private void OnRenamed(RenamedEventArgs e)
        {
            if (Renamed != null)
                Renamed(this, e);
        }

        void OnFswDeleted(object sender, FileSystemEventArgs e)
        {
            Dispatcher.BeginInvoke((Action<FileSystemEventArgs>)OnDeleted, e);
        }

        private void OnDeleted(FileSystemEventArgs obj)
        {
            if (Deleted != null)
                Deleted(this, obj);
        }

        void OnFswCreated(object sender, FileSystemEventArgs e)
        {
            Dispatcher.BeginInvoke((Action<FileSystemEventArgs>)OnCreated, e);
        }

        private void OnCreated(FileSystemEventArgs obj)
        {
            if (Created != null)
                Created(this, obj);
        }

        void OnFswChanged(object sender, FileSystemEventArgs e)
        {
            Dispatcher.BeginInvoke((Action<FileSystemEventArgs>)OnChanged, e);
        }

        private void OnChanged(FileSystemEventArgs obj)
        {
            if (Changed != null)
                Changed(this, obj);
        }

        public void Dispose()
        {
            if (_fsw != null)
            {
                _fsw.EnableRaisingEvents = false;
                DisableEvents();
                _fsw = null;
            }
        }
    }
}